package com.snail.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

    public static void saveToFile(String fileContent, String fileName) throws Exception {
        File file = new File(fileName);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        FileWriter fw = null;
        try {
            fw = new FileWriter(file);
            fw.write(fileContent);
        } finally {
            if (fw != null) {
                fw.close();
            }
        }
    }

    public static List<String> getContentFromFileByLine(File file) throws Exception {
        List<String> textList = new ArrayList<>();
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            String lineTxt = null;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                textList.add(lineTxt);
            }
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
        }
        return textList;
    }
}
